var LGDSView = Class.create({
  initialize: function (element, hexLocator, viewName) {
    // Anchor dom element
    this.anchor = $(element);
    this.hexLocator = hexLocator;
    // Remove the notification
    this.anchor.update('');
    this.anchor.setStyle({
      width: 0,
      backgroundColor: '#999999',
      top: 400,
      left: '45%'
    });
    // Reposition
    this.anchor.makePositioned();
    // view type : "playerView", "godView", ...
    this.viewName = viewName;
    // All tiles visible in current view (an Array of LGDSTile)
    this.tiles = new Array();
    this.viewSize = 0;
    // TODO what to see : items?, players?, etc... switch case: case: case:
    this.grid = TRUE;
  },
  // Get all objects in current view
  getView: function (viewSize) {
    this.tiles.clear();
    if(viewSize < 0) {
      log.addWarning("Get a Negative value for 'viewSize' : "+viewSize);
      this.viewSize = 0;
    }
    else
      this.viewSize = viewSize;
    switch(this.viewName) {
      case "playerView" :
	  this.getPlayerView();
	  break;
      // case "godView" :
      default :
	  log.addError("viewName doesn't match or not specified");
	  break;
    }
  },
  // reload all objects in current view TODO
  updateView: function () {
    // loadItems
    // loadTruc
    // ...
  },
  getPlayerView: function () {
    var info = 'tiles=';
    var pos = this.hexLocator.local2global(0, 0);
    info = info + 
	'x<=' + (this.viewSize + pos[0]) + 
	' AND y<=' + (this.viewSize + pos[1]) + 
	' AND x>=' + (-this.viewSize + pos[0]) +
	' AND y>=' + (-this.viewSize + pos[1]) + 
	' AND -1*(-x-y)>=' + (-this.viewSize + pos[0] + pos[1]) +
	' AND -1*(-x-y)<=' + (this.viewSize + pos[0] + pos[1]);
    // WARNING
    log.add("Requested tiles : "+info);
    this.askView(info);
  },
  askView: function (info) {
    // Javascript is brain-damaged, see http://news.hping.org/comp.lang.javascript.archive/0637.html
    if (!this.onSuccessHandler)
      this.onSuccessHandler = this.received.bind(this);
    new Ajax.Request('lgds_query/getView.php', {
      method: 'post',
      postBody: info,
      onSuccess: this.onSuccessHandler
    });
  },
  received: function (transport) {
    var descs = transport.responseText.evalJSON();
    for (var i = 0 ; i < descs.length ; i++) {
      var coord = this.hexLocator.global2local(descs[i].x, descs[i].y);
      // A new tile
      this.tiles[i] = new LGDSTile(coord[0], coord[1]);
      var pos = this.hexLocator.whereInPlayGround(this.tiles[i].x, this.tiles[i].y);
      // Treat each object
      for(var j = 0 ; j < descs[i].objects.length; j++) {
	var obj = descs[i].objects[j];
	switch(obj.type) {
	  case "field" :
	      var field = new LGDSField(obj.type, obj.id, obj.name, obj.desc, obj.malus);
	      field.setPos(pos[0], pos[1], 1);
	      this.tiles[i].setField(field);
	      // Set mask
	      this.tiles[i].setMask(pos[0], pos[1], 2, "field");
	      break;
	  case "wall" :
	      var wall = new LGDSWall(obj.type, obj.id, obj.name, obj.desc);
	      wall.setPos(pos[0], pos[1], 3);
	      this.tiles[i].setWall(wall);
	      // Set mask
	      this.tiles[i].setMask(pos[0], pos[1], 4, "wall");
	      break;
	  default :
	      log.addError('Object ['+obj.type+'] cannot be treat. Make sure that value match in query.');
	}
	
	// TODO with on mask or draw coord on border somewere
	//this.tiles[i].mask.element.title = descs[i].x+" "+descs[i].y; // WARNING
      }
    }
    this.show();
  },
  show: function() {
    hexLocator.sortTo3dIso(this.tiles);
    // FIXME
    this.anchor.childElements().each(Element.remove);
    for (var i = 0 ; i < this.tiles.length ; i++)
    {
      // Add a child to the DOM Hierarch
      this.anchor.appendChild(this.tiles[i].field.visual.get());
      this.anchor.appendChild(this.tiles[i].mask.get());
      if(this.tiles[i].wall.name)
	this.anchor.appendChild(this.tiles[i].wall.visual.get());
    }
  },
  getTilesInView: function () {
    return this.tiles;
  },
  eraseView: function () {
    // Remove DOM Elements
    this.anchor.childElements().each(Element.remove);
    // Delete tiles
    this.tiles.clear();
  },
  switchGrid: function () {
    this.grid = !this.grid;
    for(var i=0; i<this.tiles.length; i++) {
      if(this.grid)
	this.tiles[i].gridOn();
      else
	this.tiles[i].gridOff();
    }
  }
});
